{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c4bd3105",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3b3cc096",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1623eff8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "87751b47",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = \"fu2VRWeBpVrBmvZBWKV9abpA\"\n",
    "SECRET_KEY = \"oYvA3LHffAHHCSI0pVB6ClZgKxQhkXwN\"\n",
    "\n",
    "#文件路径\n",
    "AUDIO_FILE = 'speech-demo-master/rest-api-asr/bash_shell/16k.m4a'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a14e4e52",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.429167\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'{\"corpus_no\":\"7233693168087394297\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"北京科技馆。\"],\"sn\":\"997551819601684225436\"}\\n'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yang_token = bdasr.fetch_token(API_KEY,SECRET_KEY)\n",
    "bdasr.asr(yang_token,AUDIO_FILE)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f45b944",
   "metadata": {},
   "source": [
    "# 语音识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b07da2e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7dd2cdf0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.352681\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Hello hello。'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr_output = eval(asr(yang_token,'1.wav'))['result'][0]\n",
    "asr_output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f6f803e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
